package com.example.kotlincustomview.view

import android.content.Context
import android.util.AttributeSet
import android.view.View


import android.graphics.*
import com.example.kotlincustomview.R


class CircleImageView : View {
    private var mWidth: Int = 0
    private var mCirclePaint: Paint? = null
    private var mImagePaint: Paint? = null
    private val mRectF = RectF() //背景圆
    private val mRectF1 = RectF()//图片
    private var bitmapbg: Bitmap? = null//图片背景

    constructor(context: Context) : super(context) {
        initData()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initData()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initData()
    }

    private fun initData() {
        mCirclePaint = Paint()
        mCirclePaint?.style = Paint.Style.FILL
        mCirclePaint?.color = resources.getColor(R.color.color_ffc7ab)//默认蓝色
        mCirclePaint?.isAntiAlias = true

        mImagePaint = Paint()
        mImagePaint?.style = Paint.Style.FILL
        mImagePaint?.isAntiAlias = true

    }

    override fun onDraw(canvas: Canvas?) {
        mRectF.set(0f, 0f, mWidth.toFloat(), mWidth.toFloat())
        canvas?.drawArc(mRectF, 0f, 365f, false, mCirclePaint!!)
        if (bitmapbg != null) {
            val shader = BitmapShader(bitmapbg!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
            mImagePaint?.shader = shader
            var width = (mWidth / 4).toFloat()
            mRectF1.set(width, width, mWidth - width, mWidth - width)
            canvas?.drawRect(mRectF1, mImagePaint!!)
        }
        super.onDraw(canvas)
    }

    /**
     * 设置图片
     * */
    fun setImageView(drawable: Int, newWidth: Int, newHeight: Int) {
//        bitmapbg = zoomImg(BitmapFactory.decodeResource(resources, drawable), newWidth, newHeight)
        bitmapbg = BitmapFactory.decodeResource(resources, drawable)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var height = getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
        mWidth = getDefaultSize(suggestedMinimumWidth, widthMeasureSpec)
        var min = Math.min(mWidth, height)// 获取View最短边的长度
        setMeasuredDimension(min, min)// 强制改View为以最短边为长度的正方形
    }

    fun zoomImg(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap {
        // 获得图片的宽高
        var width = bm.width
        var height = bm.height
        // 计算缩放比例
        var scaleWidth = (newWidth / width).toFloat()
        var scaleHeight = (newHeight / height).toFloat()
        // 取得想要缩放的matrix参数
        var matrix = Matrix()
        if (scaleWidth == 0f && scaleHeight == 0f) {
            matrix.postScale(1f, 1f)
        } else {
            matrix.postScale(scaleWidth, scaleHeight)
        }
        // 得到新的图片
        var newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true)
        return newbm
    }

}